namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceHospitalPageEdit : Page
    {
        protected TextBox HospitalAddressTxb;
        protected DropDownList HospitalCityDdl;
        protected TextBox HospitalFeatureTxb;
        protected TextBox HospitalNameTxb;
        protected TextBox HospitalPhoneTxb;
        protected DropDownList HospitalProvinceDdl;
        protected Image iResource;
        protected Label lbHospitalAddress;
        protected Label lbHospitalCity;
        protected Label lbHospitalFeature;
        protected Label lbHospitalName;
        protected Label lbHospitalPhone;
        protected Label lbHospitalProvince;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["HospitalId"]);
                ResourceHospital resourceHospital = ManageResource.GetResourceHospitalInfoViaId(id);
                this.HospitalNameTxb.Text = resourceHospital.HospitalName;
                this.HospitalAddressTxb.Text = resourceHospital.HospitalAddress;
                this.HospitalPhoneTxb.Text = resourceHospital.HospitalPhone;
                this.HospitalFeatureTxb.Text = resourceHospital.HospitalFeature;
                for (int i = 0; i < this.HospitalProvinceDdl.Items.Count; i++)
                {
                    if (this.HospitalProvinceDdl.Items[i].Value == resourceHospital.HospitalProvince.ToString())
                    {
                        this.HospitalProvinceDdl.SelectedIndex = i;
                    }
                }
                this.HospitalCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceHospital.HospitalProvince);
                this.HospitalCityDdl.DataTextField = "regionName";
                this.HospitalCityDdl.DataValueField = "regionId";
                this.HospitalCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.HospitalCityDdl.Items.Count; i++)
                {
                    if (this.HospitalCityDdl.Items[i].Value == resourceHospital.HospitalCity.ToString())
                    {
                        this.HospitalCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.HospitalCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.HospitalCityDdl.Items.Count; i++)
                {
                    if (this.HospitalCityDdl.Items[i].Value == resourceHospital.HospitalCity.ToString())
                    {
                        this.HospitalCityDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void HospitalProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.HospitalCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HospitalProvinceDdl.SelectedItem.Value));
            this.HospitalCityDdl.DataTextField = "regionName";
            this.HospitalCityDdl.DataValueField = "regionId";
            this.HospitalCityDdl.DataBind();
        }

        private void InitializeComponent()
        {
            this.HospitalProvinceDdl.SelectedIndexChanged += new EventHandler(this.HospitalProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.HospitalProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.HospitalProvinceDdl.DataTextField = "RegionName";
            this.HospitalProvinceDdl.DataValueField = "RegionId";
            this.HospitalProvinceDdl.DataBind();
            this.HospitalCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HospitalProvinceDdl.SelectedItem.Value));
            this.HospitalCityDdl.DataTextField = "regionName";
            this.HospitalCityDdl.DataValueField = "regionId";
            this.HospitalCityDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strHospitalName = this.HospitalNameTxb.Text.Trim();
            string strHospitalAddress = this.HospitalAddressTxb.Text.Trim();
            string strHospitalPhone = this.HospitalPhoneTxb.Text.Trim();
            string strHospitalFeature = this.HospitalFeatureTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strHospitalName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HospitalCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HospitalCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceHospitalInfo(new Guid(base.Request.QueryString["HospitalId"]), strHospitalName, new Guid(this.HospitalProvinceDdl.SelectedItem.Value), cityId, strHospitalAddress, strHospitalPhone, strHospitalFeature);
                    base.Response.Redirect("~/CIS/Resource/ResourceHospitalPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strHospitalName != "")
            {
                if (!ManageResource.IsExistSameResourceHospitalInfo(strHospitalName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HospitalCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HospitalCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceHospitalInfo(strHospitalName, new Guid(this.HospitalProvinceDdl.SelectedItem.Value), cityId, strHospitalAddress, strHospitalPhone, strHospitalFeature);
                    base.Response.Redirect("~/CIS/Resource/ResourceHospitalPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbHospitalName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbHospitalProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalProvinceOverSea", language, path);
                this.lbHospitalCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalCityOverSea", language, path);
            }
            else
            {
                this.lbHospitalProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalProvince", language, path);
                this.lbHospitalCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalCity", language, path);
            }
            this.lbHospitalAddress.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalAddress", language, path);
            this.lbHospitalPhone.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalPhone", language, path);
            this.lbHospitalFeature.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "lbHospitalFeature", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHospitalPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

